package com.example.hello.service.impl;

import java.time.LocalDateTime;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.hello.entity.AlarmInfo;
import com.example.hello.entity.DataMonitoring;
import com.example.hello.entity.DeviceInfo;
import com.example.hello.entity.OperatingParameter;
import com.example.hello.mapper.AlarmInfoMapper;
import com.example.hello.mapper.DataMonitoringMapper;
import com.example.hello.mapper.DeviceInfoMapper;
import com.example.hello.mapper.OperatingParameterMapper;
import com.example.hello.service.DeviceDetailService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class DeviceDetailServiceImpl implements DeviceDetailService {

    private static final Logger logger = LoggerFactory.getLogger(DeviceDetailServiceImpl.class);

    @Autowired
    private DataMonitoringMapper dataMonitoringMapper;

    @Autowired
    private OperatingParameterMapper operatingParameterMapper;

    @Autowired
    private DeviceInfoMapper deviceInfoMapper;

    @Autowired
    private AlarmInfoMapper alarmInfoMapper;

    @Override
    public PageInfo<DataMonitoring> getDataMonitoring(Integer deviceId, LocalDateTime startTime, LocalDateTime endTime, int page, int limit) {
        logger.info("查询设备监控数据 - deviceId: {}, startTime: {}, endTime: {}", deviceId, startTime, endTime);
        PageHelper.startPage(page, limit);
        List<DataMonitoring> data = dataMonitoringMapper.selectByDeviceIdAndTimeRange(deviceId, startTime, endTime);
        logger.info("查询结果数量: {}", data.size());
        return new PageInfo<>(data);
    }

    @Override
    public PageInfo<OperatingParameter> getOperatingParameters(Integer deviceId, String parameter, int page, int limit) {
        logger.info("查询设备运行参数 - deviceId: {}, parameter: {}", deviceId, parameter);
        try {
            PageHelper.startPage(page, limit);
            List<OperatingParameter> parameters = operatingParameterMapper.selectByDeviceIdAndParameter(deviceId, parameter);
            logger.info("查询结果数量: {}", parameters.size());
            if (parameters.isEmpty()) {
                logger.warn("未找到匹配的数据 - deviceId: {}, parameter: {}", deviceId, parameter);
            }
            return new PageInfo<>(parameters);
        } catch (Exception e) {
            logger.error("查询设备运行参数失败", e);
            throw e;
        }
    }

    @Override
    public PageInfo<DeviceInfo> getDeviceInfo(Integer deviceId, String status, int page, int limit) {
        logger.info("查询设备信息 - deviceId: {}, status: {}", deviceId, status);
        PageHelper.startPage(page, limit);
        List<DeviceInfo> devices = deviceInfoMapper.selectByDeviceIdAndStatus(deviceId, status);
        logger.info("查询结果数量: {}", devices.size());
        return new PageInfo<>(devices);
    }

    @Override
    public PageInfo<AlarmInfo> getAlarmInfo(Integer deviceId, Boolean resolved, LocalDateTime startTime, LocalDateTime endTime, int page, int limit) {
        logger.info("查询设备报警信息 - deviceId: {}, resolved: {}, startTime: {}, endTime: {}", 
            deviceId, resolved, startTime, endTime);
        PageHelper.startPage(page, limit);
        List<AlarmInfo> alarms = alarmInfoMapper.selectByDeviceIdAndTimeRangeAndResolved(deviceId, startTime, endTime, resolved);
        logger.info("查询结果数量: {}", alarms.size());
        return new PageInfo<>(alarms);
    }
} 